/* CommandQueue
 * send means the command is going TO the socket, receive is coming FROM the socket
 * if we are a server, send goes to client, receive gets from client 
 */

package sbcc.edu.blackjack.command;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import sbcc.edu.blackjack.command.client.ClientCommand;
import sbcc.edu.blackjack.command.server.ServerCommand;
import sbcc.edu.blackjack.command.table.TableCommand;

public class CommandQueue {
	private Queue<TableCommand> tableSend;
	private Queue<TableCommand> tableReceive;
	private Queue<ClientCommand> clientSend;
	private Queue<ClientCommand> clientReceive;
	private Queue<ServerCommand> serverSend;
	private Queue<ServerCommand> serverReceive;

	
	public CommandQueue(){
		tableSend = new ConcurrentLinkedQueue<TableCommand>();
		tableReceive = new ConcurrentLinkedQueue<TableCommand>();
		
		clientSend = new ConcurrentLinkedQueue<ClientCommand>();
		clientReceive = new ConcurrentLinkedQueue<ClientCommand>();
		
		serverSend = new ConcurrentLinkedQueue<ServerCommand>();
		serverReceive = new ConcurrentLinkedQueue<ServerCommand>();
	}

	// functions for use by operators getting commands from, and sending commands to, the internet
	public void sendCommand(Command c){
		if (c instanceof TableCommand)
			tableSend.add((TableCommand) c);
		else if (c instanceof ClientCommand)
			clientSend.add((ClientCommand) c);
		else if (c instanceof ServerCommand)
			serverSend.add((ServerCommand) c);

	}
	
	public void sendCommand(TableCommand c){
		tableSend.add(c);
	}
	public void sendCommand(ClientCommand c){
		clientSend.add(c);
	}
	public void sendCommand(ServerCommand c){
		serverSend.add(c);
	}
	
	public TableCommand getTableCommand(){
		return tableReceive.poll();
	}
	public ClientCommand getClientCommand(){
		return clientReceive.poll();
	}
	public ServerCommand getServerCommand(){
		return serverReceive.poll();
	}
	
	// functions for use by iothreads reading commands from a socket and writing commands to a socket
	public void add(Command c){
		//adds a command to a queue
		if (c instanceof TableCommand)
			tableReceive.add((TableCommand) c);
		else if (c instanceof ClientCommand)
			clientReceive.add((ClientCommand) c);
		else if (c instanceof ServerCommand)
			serverReceive.add((ServerCommand) c);
	}
	
	public Command poll(){
		//this algorithm does not return commands fairly! shouldnt be a problem though
		if(!tableSend.isEmpty()){
			return tableSend.poll();
		}
		else if(!clientSend.isEmpty()){
			return clientSend.poll();
		}
		else if(!serverSend.isEmpty()){
			return serverSend.poll();
		}
		else
			return null;
	}
}
